#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <windows.h>
#include <iostream>
#include <string.h>
#include <time.h>
#include <vector>
#include<map>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")

using namespace std;


const int MAX_NUM = 3; // 最大连接人数
map<int, int> serial_number_map;	// 记录序列号和使用人数
map<int,vector<int>> serial_number_userip; // 记录序列号和使用人的IP地址
vector<int> used_nums; // 记录已经使用过的序列号






bool check_generated(int num) {
	for (int i = 0; i < used_nums.size(); ++i) {
		if (used_nums[i] == num) {
			return true; // 已经使用过
		}
	}
	return false; // 没有使用过
}


void generate_license() {


	int num = rand() % 9000000000 + 1000000000; // 随机生成一个十位数的序列号
	while (check_generated(num)) { // 如果已经使用过，重新生成
		num = rand() % 9000000000 + 1000000000;
	}
	used_nums.push_back(num);
	serial_number_map[num] = 0;
	cout << "generate_license:" << num << endl;

}



int main()
{
	int iRet;
	WORD ver = MAKEWORD(2, 2);            
	WSADATA data;
	iRet = WSAStartup(ver, &data);     

	//创建套接字
	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//服务端

	//绑定网络端口
	sockaddr_in sin = {};
	sin.sin_family = AF_INET;
	sin.sin_port = htons(6666);			

	//
	sin.sin_addr.S_un.S_addr = INADDR_ANY;		//任意的IP地址inet_addr("127.0.0.1");

	iRet = bind(sock, (sockaddr*)&sin, sizeof(sockaddr_in));

	//listen监听

	iRet = listen(sock, 5);

	//accept 等待接收客户端连接

	SOCKADDR_IN clientAddr;
	int nAddrLen = sizeof(SOCKADDR_IN);

	


	cout << "server start"<<endl;
	// 循环接收客户端连接请求并创建线程处理连接
	int threadID = 0;
	while (true)
	{
		SOCKET csock = accept(sock, (SOCKADDR*)&clientAddr, &nAddrLen); //accept接收连接

		char recv_buffer[256] = {};
		recv(csock, recv_buffer, 256, 0);

		if (strcmp(recv_buffer, "buy license")==0) {
			char send_buffer[256] = "license bought";
			generate_license();
			send(csock, send_buffer, strlen(send_buffer) + 1, 0);
		}
		else if (recv_buffer[0] == 'I') {
			cout << recv_buffer << endl;
		}
		else {

			int input_num = atoi(recv_buffer);
			bool find = false;
			for (int i = 0; i < used_nums.size(); ++i) {
				if (used_nums[i] == input_num) {
					find = true;
					break;
				}
			}

			if (!find) {
				char send_buffer[256] = "serial number error!";
				send(csock, send_buffer, strlen(send_buffer) + 1, 0);
				cout << "serial number error!" << endl;
			}
			else {
				if (serial_number_map[input_num] >= MAX_NUM) {
					char send_buffer[256] = "out of MAX_NUM!";
					send(csock, send_buffer, strlen(send_buffer) + 1, 0);
					cout << "out of MAX_NUM!" << endl;
				}
				else {
					serial_number_map[input_num] += 1;
					serial_number_userip[input_num].push_back(clientAddr.sin_addr.S_un.S_addr);
					char send_buffer[256] = "valid license";
					send(csock, send_buffer, strlen(send_buffer) + 1, 0);
					cout << "valid license" <<recv_buffer<< endl;

				}				
			}
		}

	}
	//6、清除socket环境
	iRet = closesocket(sock);

	if (iRet == SOCKET_ERROR)
	{
		cout << "closesocket fail" << endl;
	}
	else
	{
		cout << "closesocket success" << endl;
	}

	WSACleanup();//清除
	return 0;
}

